#include <irrlicht.h>
#include <iostream>
#include "Input.h"
#include "Camera.h"
#include "Fighter.h"
#include "Player.h"
#include "LogicManager.h"

using namespace irr;

using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

#ifdef _IRR_WINDOWS_
#pragma comment(lib, "Irrlicht.lib")
#endif


int main()
{
    video::E_DRIVER_TYPE driverType;

    printf("Please select the driver you want:\n"\
            " (a) Direct3D 9.0c\n (b) Direct3D 8.1\n (c) OpenGL 1.5\n"\
            " (d) Software Renderer\n (e) Burning's Software Renderer\n"\
            " (f) NullDevice\n (otherKey) exit\n\n");

    char i;
    std::cin >> i;

    switch(i) {
        case 'a': driverType = video::EDT_DIRECT3D9;break;
        case 'b': driverType = video::EDT_DIRECT3D8;break;
        case 'c': driverType = video::EDT_OPENGL;   break;
        case 'd': driverType = video::EDT_SOFTWARE; break;
        case 'e': driverType = video::EDT_BURNINGSVIDEO;break;
        case 'f': driverType = video::EDT_NULL;     break;
        default: return 1;
    }       


    core::dimension2d<s32> screenSize;

    printf("Please select a screen size:\n"\
            " (a) 800x600\n (b) 1024x768\n (c) 1280x1024\n"\
            " (otherKey) exit\n\n");

    std::cin >> i;

    switch(i) {
        case 'a': screenSize = core::dimension2d<s32>(800, 600); break;
        case 'b': screenSize = core::dimension2d<s32>(1024, 768); break;
        case 'c': screenSize = core::dimension2d<s32>(1280, 1024); break;
        case 'd': screenSize = core::dimension2d<s32>(80, 60); break;
        default: return 1;
    }

    IrrlichtDevice *device =
            createDevice(driverType, screenSize, 32, false, false, false, 0);

    if(device == 0)
        return 1;


    device->setWindowCaption(L"Mass RTS - Kill 'em all");

    IVideoDriver* driver = device->getVideoDriver();
    ISceneManager* smgr = device->getSceneManager();
    IGUIEnvironment* guienv = device->getGUIEnvironment();

	Input *receiver = new Input();
    device->setEventReceiver(receiver);
	LogicManager *lm = new LogicManager(device->getTimer());

	Camera *camera = new Camera(receiver, smgr);
	Fighter *f1 = new Fighter(smgr);
        Fighter *f2 = new Fighter(smgr);
        Fighter *f3 = new Fighter(smgr);
        Fighter *f4 = new Fighter(smgr);
        Fighter *f5 = new Fighter(smgr);
        Fighter *f6 = new Fighter(smgr);

	Player *player = new Player(receiver, device->getCursorControl(), smgr, lm);

	lm->add(camera, 2);
	lm->add(f1, 1);
        lm->add(f2, 3);
        lm->add(f3, 5);
        lm->add(f4, 7);
        lm->add(f5, 9);
        lm->add(f6, 11);

	lm->add(player, 4);

    scene::ITerrainSceneNode* terrain = smgr->addTerrainSceneNode( 
                            "../media/terrain-heightmap.bmp", 0, 101, core::vector3df(0.f, 0.f, 0.f),
                            core::vector3df(0.f, 0.f, 0.f),core::vector3df(40.f, 4.4f, 40.f),
                            video::SColor ( 255, 255, 255, 255 ),  
                            5, scene::ETPS_17, 4
                            );

    terrain->setMaterialFlag(video::EMF_LIGHTING, false);

    terrain->setMaterialTexture(0, driver->getTexture("../media/terrain-texture.jpg"));
    terrain->setMaterialTexture(1, driver->getTexture("../media/detailmap3.jpg"));

    terrain->setMaterialType(video::EMT_DETAIL_MAP);

    terrain->scaleTexture(1.0f, 20.0f);
    while(device->run())
    {
            driver->beginScene(true, true, SColor(255,100,101,140));
			lm->updateAll();
            smgr->drawAll();
            guienv->drawAll();
            driver->endScene();
    }

    device->drop();

    return 0;
}

